package company.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import company.common.R;
import company.domain.Activities;
import company.domain.Donors;
import company.domain.Rescuestations;
import company.service.DonorsService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;


@RestController
public class DonorsController {
    @Resource
    DonorsService donorsService;

    /*
     * 查询
     * */
    @RequestMapping("/selectAllDonors/{page}")
    public R selectAllDonors(@PathVariable("page") int page,@RequestBody Donors donors){
        QueryWrapper<Donors> queryWrapper=new QueryWrapper<>();
        queryWrapper.like(donors.getName()!="" && !("".equals(donors.getName())),"name",donors.getName());

        //第一个参数表示第几页，第二个参数表示每页的条数
        PageHelper.startPage(page,3);
        List<Donors> list=this.donorsService.list(queryWrapper);
        PageInfo<Object> pageInfo = new PageInfo<>(list);
        return new R().add("pageInfo",pageInfo);
    }

    /*
    * 删除
    * */
    @RequestMapping("/DelDonors/{id}")
    public R DelDonors(@PathVariable("id") int id){
        boolean remove = this.donorsService.removeById(id);
        R r=new R();
        if(remove==true){
            r.setMessage("删除成功");
        }else{
            r.setMessage("删除失败");
        }
        return r;
    }

    /*
     * 根据捐赠者id查询
     * */
    @PostMapping("/selectDonors")
    public R selectDonors(@RequestBody Donors donors){
        Donors byId = this.donorsService.getById(donors.getDonorid());
        return new R().add("byId",byId);
    }

    /*
     * 修改捐赠者
     * */
    @PostMapping("/uploadDonors")
    public R uploadDonors(@RequestBody Donors donors){
        this.donorsService.updateById(donors);
        return new R();
    }

    /*
     * 新增捐赠者
     * */
    @PostMapping("/AddDonors")
    public R AddDonors(@RequestBody Donors donors){
        this.donorsService.save(donors);
        return new R();
    }
}
